package com.ruoyi.flowable.controller;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.flowable.controller.base.BaseFlowableController;
import com.ruoyi.flowable.entity.*;
import com.ruoyi.flowable.services.ProcessEngineService;
import com.ruoyi.flowable.util.FlowUtils;
import org.flowable.bpmn.model.CustomProperty;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
 * dustball  2021年4月11日
 *
 * 流程提交前段参数接收、解析、封装
 *
 */

@RestController
@RequestMapping("/process")
public class ProcessEngineController extends BaseFlowableController {

    @Autowired
    private ProcessEngineService processEngineService;

    //预提交处理单
    @GetMapping("/preSubmit/{processId}/{id}")
    public AjaxResult preSubmit(@PathVariable String processId, @PathVariable String id, Map<String, Object> map) {
        return null;
    }

    //预提交处理单2
    @GetMapping("/preSubmitForRouter/{processId}/{id}")
    public AjaxResult preSubmitForRouter(@PathVariable String processId, @PathVariable String id, Map<String, Object> map) {
        Map<String, List<UserTask>> tempMap = processEngineService.preSubmitForRouter(processId, id, map);
        //数据加工处理，主要处理UserTask节点
        ProcessFormDataEntity processFormDataEntity = new ProcessFormDataEntity();
        int x = 0;
        for (Map.Entry<String, List<UserTask>> entry : tempMap.entrySet()) {
            RouterDataEntity routerDataEntity = new RouterDataEntity();
            routerDataEntity.setId(x++);
            routerDataEntity.setRouterName(entry.getKey());
            for (UserTask userTask : entry.getValue()) {
                UserTasksEntity userTasksEntity = new UserTasksEntity();
                userTasksEntity.setUserTaskId(userTask.getId());
                userTasksEntity.setUserTaskName(userTask.getName());

                //获取扩展属性
                List<CustomProperty> customProps=userTask.getCustomProperties();
                for(CustomProperty customProperty:customProps){
                   //开始判断属性

                }

                //暂时全部设置为null,与小伟对接时，再深入
                userTasksEntity.setUserChoiceType("null");
                //单选,深化用
//                userTasksEntity.setSingleUser(true);
//                扩展参数深化用
//                userTasksEntity.setExtendParams();
                routerDataEntity.getUserTasks().add(userTasksEntity);
            }
            processFormDataEntity.getRouterData().add(routerDataEntity);
        }
        //处理常用意见

        System.out.println(processFormDataEntity);
        return AjaxResult.success(processFormDataEntity);
    }


}
